package com.zlt.appparking.controller;

import com.zlt.appparking.service.LicenseService;
import com.zlt.appparking.utils.FileUploadUtil;
import com.zlt.appparking.utils.StringUtil;
import com.zlt.appparking.vo.LicenseVO;
import com.zlt.appparking.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("license")
public class LicenseController {

    @Autowired
    private LicenseService licenseService;

    /**
     * 申述车牌，上传行驶证
     * @param file 行驶证图片
     * @return
     */
    @PostMapping("appeal")
    public Result appealLicense(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        boolean success = false;
//        String root = getClass().getClassLoader().getResource("static/files").getPath();
        if(StringUtil.isNotNull(request.getParameter("lid"))){//车牌id不为空
            Long lid =Long.parseLong(request.getParameter("lid")) ;
            String path= new FileUploadUtil().uploadQNY(file);//返回文件保存路径
            success = licenseService.appealLicence(path,lid);
        }
        return success ? Result.ok("提交成功") : Result.ok("提交失败");
    }

    /**
     * 绑定车牌，只传车牌号
     * @param number
     * @return
     */
    @PostMapping("bind")
    public Result bindLicense(String number){
        boolean success = licenseService.bindLicensePlate(number);
        return success ? Result.ok("提交审核成功") : Result.ok("提交审核失败");
    }

    /**
     * 查看已绑定车牌号
     * @return
     */
    @PostMapping("license")
    public Result licenses(){
        List<LicenseVO> licenses = licenseService.licenses();
        return Result.ok("查询成功",licenses);
    }

    /**
     * 查询车牌数据详情
     * @param id
     * @return
     */
    public Result detail(Long id){
        LicenseVO licenseVO = licenseService.selectLicense(id);
        return Result.ok("查询成功",licenseVO);
    }

}
